package com.estun.backroundManagementService.modules.position.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.modules.position.model.Factory;
import com.estun.backroundManagementService.modules.position.service.FactoryService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 工厂
 */
@RestController
@RequestMapping("/factory")
@RequiredArgsConstructor
public class FactoryController {

    private final FactoryService factoryService;

    //分页查询物料信息
    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<Factory> pageRequest) {
        Page<Factory> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<Factory> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<Factory> page1 = factoryService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }

    // 根据ID获取物料信息
    @GetMapping("/{id}")
    public BaseResponse getFactoryById(@PathVariable String id) {
        return BaseResponse.success(factoryService.getById(id));
    }

    // 添加物料信息
    @PostMapping("/add")
    @Log(module = "工厂管理 新增", type = LogConstants.INSERT, desc = "工厂管理 新增")
    public BaseResponse addFactory(@RequestBody Factory factory) {
        return BaseResponse.success(factoryService.save(factory));
    }

    // 更新物料信息
    @PutMapping("/update")
    @Log(module = "工厂管理 更新", type = LogConstants.UPDATE, desc = "工厂管理 更新")
    public BaseResponse updateFactory(@RequestBody Factory factory) {
        return BaseResponse.success(factoryService.updateById(factory));
    }

    // 删除物料信息
    @DeleteMapping("/delete/{id}")
    @Log(module = "工厂管理 删除", type = LogConstants.DELETE, desc = "工厂管理 删除")
    public BaseResponse deleteFactory(@PathVariable String id) {
        return BaseResponse.success(factoryService.removeById(id));
    }


    @ApiOperation("查询所有")
    @GetMapping(value = "/list")
    @ResponseBody
    public BaseResponse list() {
        return BaseResponse.success(factoryService.list());
    }




}
